package com.nanyang.tms.api.dto.request.saleproduct.update;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
public class SaleProductBaseUpdateRequest extends ApiRequest {

    @JsonIgnore
    private String pin;
    @Valid
    @NotNull(message = "saleProductUpdateRequest不能为空")
    private SaleProductUpdateRequest saleProductUpdateRequest;
    @Valid
    @NotNull(message = "saleProductCarryRuleUpdateRequestList不能为空")
    private List<SaleProductCarryRuleUpdateRequest> saleProductCarryRuleUpdateRequestList;
    @Valid
    @NotNull(message = "saleProductWeightModelUpdateRequest不能为空")
    private SaleProductWeightModelUpdateRequest saleProductWeightModelUpdateRequest;
    @Override
    public void requestCheck() {
            //1、产品计重规则表 国家、城市、货物类型唯一；国家、城市为空时候设置未ALL，有城市信息 国家信息不能为空
            List<String> checkWeightModelList=new ArrayList<>();
            for (SaleProductWeightRuleUpdateRequest saleProductWeightRuleUpdateRequest : saleProductWeightModelUpdateRequest.getSaleProductWeightRuleUpdateRequestList()) {
                if(StringUtils.isNotEmpty(saleProductWeightRuleUpdateRequest.getCityCode())&&StringUtils.isEmpty(saleProductWeightRuleUpdateRequest.getCountyCode())){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则中国家信息不能为空！");
                }
                if(StringUtils.isEmpty(saleProductWeightRuleUpdateRequest.getCityCode())){
                    saleProductWeightRuleUpdateRequest.setCityCode("ALL");
                    saleProductWeightRuleUpdateRequest.setCityName("ALL");
                }
                if(StringUtils.isEmpty(saleProductWeightRuleUpdateRequest.getCountyCode())){
                    saleProductWeightRuleUpdateRequest.setCountyCode("ALL");
                    saleProductWeightRuleUpdateRequest.setCountyName("ALL");
                }
                String weightModelStr=saleProductWeightRuleUpdateRequest.getCountyCode()+"-"+saleProductWeightRuleUpdateRequest.getCityCode()+"-"+saleProductWeightRuleUpdateRequest.getCargoTypeCode();
                if(checkWeightModelList.contains(weightModelStr)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则 国家、城市、货物类型信息唯一，不能存在重复");
                }
                checkWeightModelList.add(weightModelStr);
           }
            //2、计费重进位规则 起始重量和截止重量 区间唯一
            List<String> checkCarryRuleList=new ArrayList<>();
            for (SaleProductCarryRuleUpdateRequest saleProductCarryRuleUpdateRequest : saleProductCarryRuleUpdateRequestList) {
                String carryRuleStr=saleProductCarryRuleUpdateRequest.getWeightStart().toString()+"-"+saleProductCarryRuleUpdateRequest.getWeightEnd().toString();
                if(checkCarryRuleList.contains(carryRuleStr)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计费重进位规则 起始重量和截止重量区间唯一，不能存在重复");
                }
                checkCarryRuleList.add(carryRuleStr);
            }

    }
}
